package com.znufe.exam.service.h5;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.znufe.exam.domain.h5.grade.H5ExamInfoOfCourseOfClassResponseObject;
import com.znufe.exam.domain.h5.grade.H5ExamQuestionScoreSituationObject;
import com.znufe.exam.domain.h5.grade.H5StudentExamDetailRequestObject;
import com.znufe.exam.domain.h5.grade.H5StudentExamQuestionAnswerObject;
import com.znufe.exam.repository.h5.H5ExamGradeMapper;
import com.znufe.exam.utility.ModelMapperSingle;
import com.znufe.exam.viewmodel.h5.H5ClassExamDotimeRequestVM;
import com.znufe.exam.viewmodel.h5.H5ClassExamDotimeResponseVM;
import com.znufe.exam.viewmodel.h5.grade.*;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class H5ExamGradeServiceImpl implements H5ExamGradeService {

    private final H5ExamGradeMapper h5examGradeMapper;
    protected final static ModelMapper modelMapper = ModelMapperSingle.Instance();

    @Autowired
    public H5ExamGradeServiceImpl(H5ExamGradeMapper h5examGradeMapper) {
        this.h5examGradeMapper = h5examGradeMapper;
    }

    @Override
    public List<H5ExamPaperOfCourseResponseVM> selectExamPaperListByCourseId(Integer courseId) {
        return selectExamPaperListByCourseId(courseId);
    }

    @Override
    public List<H5CourseContentResponseVM> selectCourseInfoByClassId(Integer classId) {
        return selectCourseInfoByClassId(classId);
    }

    @Override
    public String selectClassNameByClassId(Integer classId) {
        return selectClassNameByClassId(classId);
    }

    @Override
    public H5CourseContentOfClassResponseVM getCourseContentByClassId(Integer classId) {
        H5CourseContentOfClassResponseVM vm = new H5CourseContentOfClassResponseVM();
        vm.setClassId(classId);

        String className = h5examGradeMapper.selectClassNameByClassId(classId);
        vm.setClassName(className);
        // 取出班级所上过的课程
        List<H5CourseContentResponseVM> courseContentList = h5examGradeMapper
                .selectCourseInfoByClassId(classId);
        // 遍历courseContentList,找出每一个courseContent中课程对应的试卷
        for (H5CourseContentResponseVM courseContent : courseContentList) {
            Integer courseId = courseContent.getCourseId();
            List<H5ExamPaperOfCourseResponseVM> examPaperList = h5examGradeMapper.
                    selectExamPaperListByCourseId(courseId);
            courseContent.setExamPaperList(examPaperList);
        }
        vm.setCourseContentList(courseContentList);
        return vm;
    }

    @Override
    public List<H5ClassCourseGradeResponseVM> selectClassCourseGrade(H5ClassCourseGradeRequestVM requestVM) {
        return h5examGradeMapper.selectClassCourseGrade(requestVM);
    }

    @Override
    public List<H5ClassExamGradeResponseVM> selectClassExamGrade(H5ClassExamGradeRequestVM requestVM) {
        return h5examGradeMapper.selectClassExamGrade(requestVM);
    }

    @Override
    public List<H5ClassExamDotimeResponseVM> selectClassExamDoTime(H5ClassExamDotimeRequestVM requestVM) {
        return h5examGradeMapper.selectClassExamDoTime(requestVM);
    }

    @Override
    public Integer selectClassIdByClassName(String className) {
        return h5examGradeMapper.selectClassIdByClassName(className);
    }

    @Override
    public String selectCourseNameByCourseId(Integer courseId) {
        return h5examGradeMapper.selectCourseNameByCourseId(courseId);
    }

    @Override
    public  Integer selectCourseIdByCourseName(String courseName) {
        return h5examGradeMapper.selectCourseIdByCourseName(courseName);
    }

    @Override
    public List<H5StudentIndividualGradeResponseVM> selectStudentIndividualGrade(H5StudentIndividualGradeRequestVM requestVM) {
        return h5examGradeMapper.selectStudentIndividualGrade(requestVM);
    }

    @Override
    public String selectExamPaperNameByExamPaperId(Integer examPaperId) {
        return h5examGradeMapper.selectExamPaperNameByExamPaperId(examPaperId);
    }

    @Override
    public List<H5StudentIndividualGradeResponseVM> getStudentIndividualGradeInfo(H5StudentIndividualGradeRequestVM requestVM) {
        /* // 以下注释掉的代码块 是为了将查询请求VM中的名字 换成 与名字相对应的id
        // 拿到requestVM里面的stuName
        StudentIndividualGradeRequestObject obj = modelMapper
                .map(requestVM, StudentIndividualGradeRequestObject.class);

        Integer classId = examGradeMapper.selectClassIdByClassName(requestVM.getClassName());
        obj.setClassId(classId);
        // 判断前端是否输入了课程名称，若输入，则obj中要拿到课程对应的id
        if (null != requestVM.getCourseName()) {
            Integer courseId = examGradeMapper
                    .selectCourseIdByCourseName(requestVM.getCourseName());
            obj.setCourseId(courseId);
        }
        */
        // 拿到ResponseVM里所需的六个字段，还有两个字段将在for循环中获取
        List<H5StudentIndividualGradeResponseVM> individualGradeList = h5examGradeMapper
                .selectStudentIndividualGrade(requestVM);

        for (H5StudentIndividualGradeResponseVM individualGrade : individualGradeList) {
            Integer courseId = individualGrade.getCourseId();
            /*
            String courseName = requestVM.getCourseName() == null ?
                    examGradeMapper.selectCourseNameByCourseId(courseId) :
                    requestVM.getCourseName();
                    */
            String courseName = h5examGradeMapper.selectCourseNameByCourseId(courseId);
            individualGrade.setCourseName(courseName);

            Integer examPaperId = individualGrade.getExamPaperId();
            String examPaperName = h5examGradeMapper
                    .selectExamPaperNameByExamPaperId(examPaperId);
            individualGrade.setExamPaperName(examPaperName);
        }
        return individualGradeList;
    }

    @Override
    public H5StudentExamQuestionAnswerObject selectStuAns(H5StudentExamDetailRequestObject requestObject) {
        return h5examGradeMapper.selectStuAns(requestObject);
    }

    @Override
    public List<Integer> selectExamPaperIdByClassId(Integer classId) {
        return h5examGradeMapper.selectExamPaperIdByClassId(classId);
    }

    @Override
    public Map<Integer, String> getExamPaperNameMap(List<Integer> examPaperIds) {
        Map<Integer, String> examPaperNameMap = new HashMap<>();
        List<HashMap<String, Object>> toRealMapList = h5examGradeMapper
                .getExamPaperNameMap(examPaperIds);
        if (toRealMapList != null && !toRealMapList.isEmpty()) {
            for (HashMap<String, Object> map : toRealMapList) {
                Integer id = null;
                String name = null;
                for (Map.Entry<String, Object> entry : map.entrySet()) {
                    if ("key".equals(entry.getKey())) {
                        id = (Integer)entry.getValue();
                    } else if ("value".equals(entry.getKey())) {
                        name = (String)entry.getValue();
                    }
                }
                examPaperNameMap.put(id, name);
            }
        }
        return examPaperNameMap;
    }

    @Override
    public PageInfo<H5ExamInfoOfCourseOfClassResponseObject> selectExamInfoOfCourseOfClass(H5ExamInfoOfCourseOfClassRequestVM requestVM) {
        return PageHelper.startPage(requestVM.getPageNum(),requestVM.getPageSize())
                .doSelectPageInfo(() ->
                        h5examGradeMapper.selectExamInfoOfCourseOfClass(requestVM));
    }

    @Override
    public H5ExamGradeDistributionResponseVM selectExamGradeDistribution(H5ExamGradeDistributionRequestVM requestVM) {
        return h5examGradeMapper.selectExamGradeDistribution(requestVM);
    }

    @Override
    public List<H5ExamQuestionScoreSituationObject> selectExamQusScoreSituation(H5ExamGradeDistributionRequestVM requestVM) {
        return h5examGradeMapper.selectExamQusScoreSituation(requestVM);
    }
}
